﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Software41.Tracking.Data.BAMWebService;
using System.Net;

namespace Software41.Tracking.Data
{
    public class BAMRepository : IBAMRepository
    {
        private readonly string bamServerName;

        public BAMRepository(string bamServerName)
        {
            this.bamServerName = bamServerName;
        }

        string IBAMRepository.GetReferenceData(string referenceDataUrl)
        {
            return this.GetReferenceData(referenceDataUrl);
        }

        Dictionary<string, List<string>> IBAMRepository.GetActivityInstanceIdsForRelatedActivity(string activityName, string activityId, string viewName)
        {
            return this.GetActivityInstanceIdsForRelatedActivity(activityName, activityId, viewName);
        }



        private string GetReferenceData(string referenceDataUrl)
        {
            string url = string.Format("http://{0}{1}", this.bamServerName, referenceDataUrl);

            string text;
            using (WebClient client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                text = client.DownloadString(url);
            }


            return text;
        }

        private Dictionary<string, List<string>> GetActivityInstanceIdsForRelatedActivity(string activityName, string activityId, string viewName)
        {
            GetRelatedActivityInstancesResponse relatedActivitesResp = null;

            BamManagementServiceSoapClient client = new BamManagementServiceSoapClient();
            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

            try
            {
                var relatedActivitiesReq = new GetRelatedActivityInstancesRequest
                {
                    activityInstanceId = activityId,
                    activityName = activityName,
                    viewName = viewName,
                };

                relatedActivitesResp = client.GetRelatedActivityInstances(relatedActivitiesReq);
                client.Close();
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (client.State == System.ServiceModel.CommunicationState.Faulted)
                    client.Abort();
            }


            var returnList = new Dictionary<string, List<string>>();
            List<string> listToAddTo = null;
            foreach (var ai in relatedActivitesResp.GetRelatedActivityInstancesResult.ActivityInstances)
            {
                //see if we have a list
                if (!returnList.ContainsKey(ai.ActivityName))
                    returnList.Add(ai.ActivityName, new List<string>());
                    
                listToAddTo = returnList[ai.ActivityName];

                listToAddTo.AddRange(ai.Instance.ToList<string>());

            }

            return returnList;
        }
    }
}
